﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
 
public class AjaxResult
{ 
    private AjaxResult()
    {
    }

    private bool error = false;

    /// <summary>
    /// 是否产生错误
    /// </summary>
    public bool Error { get { return error; } }

    /// <summary>
    /// 错误信息，或者成功信息
    /// </summary>
    public string Message { get; set; }

    /// <summary>
    /// 成功可能时返回的数据
    /// </summary>
    public object Data { get; set; }

    #region Error
    public static AjaxResult CreateError()
    {
        return new AjaxResult()
        {
            error  = true
        };
    }
    public static AjaxResult CreateError(string message)
    {
        return new AjaxResult()
        {
            error = true,
            Message = message
        };
    }
    #endregion

    #region Success
    public static AjaxResult Success()
    {
        return new AjaxResult()
        {
            error = false
        };
    }
    public static AjaxResult Success(string message)
    {
        return new AjaxResult()
        {
            error = false,
            Message = message
        };
    }
    public static AjaxResult Success(object data)
    {
        return new AjaxResult()
        {
            error = false,
            Data = data
        };
    }
    public static AjaxResult Success(object data,string message)
    {
        return new AjaxResult()
        {
            error = false,
            Data = data,
            Message = message
        };
    }
    #endregion

    public override string ToString()
    {
        return new JavaScriptSerializer().Serialize(this);
    }
}